package main

import (
	"bytes"
	"crypto/md5"
	"encoding/json"
	"fmt"
	"region/work/lollab"
	//"io/ioutil"
	"mime/multipart"
	"net/http"
	"net/url"
	"strings"
)

func main() {
	// form := url.Values{
	// 	"grant_type":    {"client_credentials"},
	// 	"client_id":     {"Exorcism-Union"},
	// 	"client_secret": {"PjyQ8wetz9zcB432"},
	// 	"scope":         {"member/account,member/token,member/checkout"},
	// }

	// res, err := http.PostForm("https://a1-s-point.lollab.co/api/token", form)
	// if err != nil {
	// 	fmt.Println(err)
	// 	return
	// }

	// res_body := &bytes.Buffer{}
	// _, err = res_body.ReadFrom(res.Body)
	// if err != nil {
	// 	fmt.Println(err)
	// 	return
	// }
	// res.Body.Close()
	// fmt.Println(res_body)
	str, err := lollab.DefaultLollab.Token()
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(str)
	}
}

func demo2() {
	form := url.Values{
		"grant_type":    {"client_credentials"},
		"client_id":     {"Exorcism-Union"},
		"client_secret": {"PjyQ8wetz9zcB432"},
		"scope":         {"member/account,member/token,member/checkout"},
	}

	body := strings.NewReader(form.Encode())
	req, err := http.NewRequest("POST", "https://a1-s-point.lollab.co/api/token", body)
	if err != nil {
		fmt.Println(err)
		return
	}
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	// req.Header.Set("Content-Type", "multipart/form-data")
	client := http.Client{}
	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return
	}

	res_body := &bytes.Buffer{}
	_, err = res_body.ReadFrom(res.Body)
	if err != nil {
		fmt.Println(err)
		return
	}
	res.Body.Close()
	fmt.Println(res_body)
}

func version() {
	params := make(map[string]string)
	params["request_public_key"] = "5az7ryu4nf6c08su"
	params["param1"] = "Foo"
	params["param2"] = "Bar"

	str_Data, _ := json.Marshal(params)
	params["md5"] = fmt.Sprintf("%x", md5.Sum(str_Data))
	bodyData, _ := json.Marshal(params)
	body := &bytes.Buffer{}
	body.Write(bodyData)

	req, err := http.NewRequest("Get", "https://a1-s-point.lollab.co/api/version", body)
	if err != nil {
		fmt.Println(err)
		return
	}
	// req.Form.Add("client_id", "Exorcism-Union")
	// req.Form.Add("client_secret", "PjyQ8wetz9zcB432")
	// req.Form.Add("request_public_key", "5az7ryu4nf6c08su")

	client := http.Client{}
	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return
	}

	res_body := &bytes.Buffer{}
	_, err = res_body.ReadFrom(res.Body)
	if err != nil {
		fmt.Println(err)
		return
	}
	res.Body.Close()
	fmt.Println(res_body)
}

func ServerToken() {
	body := &bytes.Buffer{}
	write := multipart.NewWriter(body)

	write.WriteField("grant_type", "client_credentials")
	write.WriteField("client_id", "Exorcism-Union")
	write.WriteField("client_secret", "PjyQ8wetz9zcB432")
	write.WriteField("scope", "member/account,member/token,member/checkout")
	write.Boundary()

	req, err := http.NewRequest("Post", "https://a1-s-point.lollab.co/api/token", body)
	if err != nil {
		fmt.Println(err)
		return
	}
	req.Header.Add("Content-Disposition", "multipart/form-data")

	// form := url.Values{
	// 	"grant_type":    {"client_credentials"},
	// 	"client_id":     {"Exorcism-Union"},
	// 	"client_secret": {"PjyQ8wetz9zcB432"},
	// 	"scope":         {"member/account,member/token,member/checkout"},
	// }
	// req.Body = ioutil.NopCloser(strings.NewReader(form.Encode()))

	// req.Form = make(map[string][]string)
	// req.Form.Add("grant_type", "client_credentials")
	// req.Form.Add("client_id", "Exorcism-Union")
	// req.Form.Add("client_secret", "PjyQ8wetz9zcB432")
	// req.Form.Add("scope", "member/account,member/token,member/checkout")

	// req.PostForm = make(map[string][]string)
	// req.PostForm.Add("grant_type", "client_credentials")
	// req.PostForm.Add("client_id", "Exorcism-Union")
	// req.PostForm.Add("client_secret", "PjyQ8wetz9zcB432")
	// req.PostForm.Add("scope", "member/account,member/token,member/checkout")

	client := http.Client{}
	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return
	}

	res_body := &bytes.Buffer{}
	_, err = res_body.ReadFrom(res.Body)
	if err != nil {
		fmt.Println(err)
		return
	}
	res.Body.Close()
	fmt.Println(res_body)
}

func api() {
	body := &bytes.Buffer{}
	write := multipart.NewWriter(body)

	write.WriteField("client_id", "Exorcism-Union")
	write.WriteField("client_secret", "PjyQ8wetz9zcB432")
	write.WriteField("request_public_key", "5az7ryu4nf6c08su")
	write.Boundary()

	req, err := http.NewRequest("Get", "https://a1-s-point.lollab.co/api", body)
	if err != nil {
		fmt.Println(err)
		return
	}
	// req.Form.Add("client_id", "Exorcism-Union")
	// req.Form.Add("client_secret", "PjyQ8wetz9zcB432")
	// req.Form.Add("request_public_key", "5az7ryu4nf6c08su")

	client := http.Client{}
	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return
	}

	res_body := &bytes.Buffer{}
	_, err = res_body.ReadFrom(res.Body)
	if err != nil {
		fmt.Println(err)
		return
	}
	res.Body.Close()
	fmt.Println(res_body)
}

func demo1() {
	body := &bytes.Buffer{}
	write := multipart.NewWriter(body)

	write.WriteField("grant_type", "client_credentials")
	write.WriteField("client_id", "Exorcism-Union")
	write.WriteField("client_secret", "PjyQ8wetz9zcB432")
	write.WriteField("scope", "member/account,member/token,member/checkout")
	write.Close()

	req, err := http.NewRequest("POST", "https://a1-s-point.lollab.co/api/token", body)
	if err != nil {
		fmt.Println(err)
		return
	}
	req.Header.Add("Content-Disposition", "multipart/form-data")

	client := http.Client{}
	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return
	}

	res_body := &bytes.Buffer{}
	_, err = res_body.ReadFrom(res.Body)
	if err != nil {
		fmt.Println(err)
		return
	}
	res.Body.Close()
	fmt.Println(res_body)
}
